package org.bb.imserver.controller;

import org.bb.im.packet.HelloPacket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tio.core.Tio;
import org.tio.core.starter.TioServerBootstrap;

@RestController
public class HelloController {
    @Autowired
    private TioServerBootstrap bootstrap;

    @GetMapping("/")
    public String index() {
        return "Hello, tio-spring-boot-starter !!!";
    }

    @GetMapping("/push")
    public String pushMessage() throws Exception {
        HelloPacket packet = new HelloPacket();
        packet.setBody("This message is pushed by Tio Server.".getBytes(HelloPacket.CHARSET));
        Tio.sendToAll(bootstrap.getServerGroupContext(), packet);
        return "Push a message to client successfully";
    }
}
